package com.xci.core.configuration;

import com.xci.core.datasource.DynamicDataSource;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;

import javax.sql.DataSource;
import java.util.HashMap;
import java.util.Map;

/**
 * 配置多数据源
 * @author 吕艳阳
 */
@Configuration
@ConditionalOnProperty(name = "xci.platform.dynamic-datasource.enabled", havingValue = "true")
public class DynamicDataSourceConfig {

    @Bean("primary")
    // @ConfigurationProperties(prefix = "spring.datasource.primary")
    public DataSource primaryDataSource() {
        return DataSourceBuilder.create().build();
    }

    @Bean("secondary")
    // @ConfigurationProperties(prefix = "spring.datasource.secondary")
    public DataSource secondaryDataSource() {
        return DataSourceBuilder.create().build();
    }

    @Primary
    @Bean(name = "dynamicDataSource")
    public DynamicDataSource dynamicDataSource() {
        Map<Object, Object> targetDataSources = new HashMap<>();
        targetDataSources.put("primary", primaryDataSource());
        targetDataSources.put("secondary", secondaryDataSource());
        return new DynamicDataSource(primaryDataSource(), targetDataSources);
    }

    // /**
    //  * 手动创建数据源
    //  */
    // public DataSource customCreateDataSource() {
    //     DataSourceBuilder dataSourceBuilder = DataSourceBuilder.create();
    //     dataSourceBuilder.driverClassName("org.h2.Driver");
    //     dataSourceBuilder.url("jdbc:h2:mem:test");
    //     dataSourceBuilder.username("SA");
    //     dataSourceBuilder.password("");
    //     return dataSourceBuilder.build();
    // }

    /**
     * 配置@Transactional注解事务
     */
    @Bean
    @Primary
    public PlatformTransactionManager txManager() {
        return new DataSourceTransactionManager(dynamicDataSource());
    }

    @Bean
    public SqlSessionFactoryBean sqlSessionFactoryBean() throws Exception {
        SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
        // 配置数据源，此处配置为关键配置，如果没有将 dynamicDataSource作为数据源则不能实现切换
        sessionFactory.setDataSource(dynamicDataSource());
        // sessionFactory.setTypeAliasesPackage("com.**.model");    // 扫描Model
        // PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
        // sessionFactory.setMapperLocations(resolver.getResources("classpath*:**/*.xml"));    // 扫描映射文件
        return sessionFactory;
    }

    @Bean
    public JdbcTemplate primaryJdbcTemplate() {
        return new JdbcTemplate(primaryDataSource());
    }

    @Bean
    public JdbcTemplate secondaryJdbcTemplate() {
        return new JdbcTemplate(secondaryDataSource());
    }
}